package com.open.capacity.reimburse.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.dao.StdCarDao;
import com.open.capacity.reimburse.entity.StdCar;
import com.open.capacity.reimburse.entity.StdTraffic;
import com.open.capacity.reimburse.service.StdCarService;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.user.model.SysArea;
import com.open.capacity.user.service.SysAreaService;
import com.open.capacity.utils.SysUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
@Service
public class StdCarServiceImpl implements StdCarService {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private StdCarDao stdCarDao;

    @Autowired
    private SysAreaService areaService;

    /**
     * 添加
     *
     * @param stdCar
     */
    public int save(StdCar stdCar) throws Exception {
        SysUser loginUser = SysUserUtil.getLoginAppUser();

        validition(stdCar);
        SysArea province = areaService.findById(stdCar.getProvinceId());
        stdCar.setProvinceName(province.getAreaName());

        stdCar.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        stdCar.setUpdateUserid(loginUser.getId());
        stdCar.setUpdateUsername(loginUser.getRealName());
        stdCar.setUpdateTime(new Date());
        return stdCarDao.save(stdCar);
    }

    private void validition(StdCar stdCar) throws Exception {
        StdCar exists = stdCarDao.findByProvinceId(stdCar.getProvinceId());
        if (exists != null) {
            throw new Exception("不允许同一个省份配置多次");
        }
    }

    /**
     * 修改
     *
     * @param stdCar
     */
    public int update(StdCar stdCar) {
        SysArea province = areaService.findById(stdCar.getProvinceId());
        stdCar.setProvinceName(province.getAreaName());

        stdCar.setUpdateTime(new Date());
        return stdCarDao.update(stdCar);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return stdCarDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public StdCar findById(String id) {
        return stdCarDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<StdCar> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<StdCar> list = stdCarDao.findAll(params);

        PageInfo<StdCar> pageInfo = new PageInfo(list);

        return PageResult.<StdCar>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }


    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<StdCar> findAll(Map<String, Object> params) {
        return stdCarDao.findAll(params);
    }

    /**
     * 校验住宿费是否超标
     *
     * @param userId     当事人
     * @param provinceId 省份
     * @param level      职级
     * @param money      金额
     * @return
     */
    @Override
    public boolean verify(String userId, String provinceId, String level, BigDecimal money) {
        StdCar item = stdCarDao.findByProvinceId(provinceId);
        boolean flag = true;
        switch (level) {
            case "1"://正厅
                flag = money.compareTo(item.getLevel1Standard()) <= 0;
                break;
            case "2"://副厅
                flag = money.compareTo(item.getLevel2Standard()) <= 0;
                break;
            case "3"://正处
                flag = money.compareTo(item.getLevel3Standard()) <= 0;
                break;
            case "4"://副处
                flag = money.compareTo(item.getLevel4Standard()) <= 0;
                break;
            case "5"://正科
                flag = money.compareTo(item.getLevel5Standard()) <= 0;
                break;
            case "6"://副科
                flag = money.compareTo(item.getLevel6Standard()) <= 0;
                break;
            case "7"://科员
                flag = money.compareTo(item.getLevel7Standard()) <= 0;
                break;
        }
        return flag;
    }
}
